// pages/school/school.js
var app = getApp();
var self;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    letter: ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'],
    school: [],
    letters: [],
    toView: "letview"
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
      self = this;
      this.getSchool();
  },
  getSchool:function(){

    wx.request({
      url: app.globalData.apiUrl + "IS00/Login.ashx?Method=XueXiaoList",
      data: {},
      method: 'GET',
      success: function (res) {
        
        if (res.data.state == 1) {
          var tmp = res.data.retData.SchoolList;
          var school = [];
          
          for(var i=0;i<tmp.length;i++){
              var obj = {};
              obj.id = tmp[i].ItemId;
              obj.name = tmp[i].ItemValue;
              obj.url = tmp[i].ItemName;
              obj.firstl = tmp[i].ItemLetter;
              obj.check = false;
              school.push(obj); 
          }
          
          var tmplett = "";
          for (var i = 0; i < school.length; i++) {
            // if (sel_country.indexOf(countrys[i].id) != -1) {
            //   countrys[i].check = true;
            // } else {
            //   countrys[i].check = false;
            // }
            tmplett += school[i].firstl;
          }
          var letter = self.data.letter;
          var letters = Array();
          for (var i = 0; i < letter.length; i++) {
            if (tmplett.indexOf(letter[i]) != -1) {
              letters.push(letter[i]);
            }
          }
          self.setData({
            school: school,
            letters: letters
          });

          wx.setStorageSync('school', school)
        }
      }
    });

    
  },
  scrollTolet: function (e) {
    var letter = e.target.dataset.let;
    this.setData({
      toView: "letview" + letter
    });
  },
  onChangeShowState: function (e) {
    var id = e.currentTarget.dataset.id;
    var value = e.currentTarget.dataset.value;
    var school = this.data.school;

    //单选
    for (var i = 0; i < school.length; i++) {
        if (school[i].id == id) {
          school[i].check = true;
          wx.setStorageSync("school", school[i]);
          wx.switchTab({
            url: '/pages/index/index',
          })
        } 
        else {
          school[i].check = false;
        }
      }
   

    this.setData({
      school: school
    });
    
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})